package com.htc.controller;


import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.htc.model.InternalResponse;
import com.htc.model.User;
import com.htc.model.request.UserRequest;
import com.htc.service.UserService;

@RestController
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("login")
	public InternalResponse login(HttpServletRequest request){
		InternalResponse internalResponse = null;
	    try{
	        User user = new User();
            user.setLogin_name(request.getParameter("username"));
            user.setLogin_password(request.getParameter("password"));
            User user2 = userService.login(user);
            if(user2!=null){
            	request.getSession().setAttribute("userkey", user2);
            	internalResponse = InternalResponse.success();
            }else{
            	internalResponse = InternalResponse.fail();
            }
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	@RequestMapping("queryList")
	public InternalResponse queryList(UserRequest request){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(userService.queryList(request));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	@RequestMapping("save")
	public InternalResponse save(User user){
		InternalResponse internalResponse = null;
	    try{
	    	if(user.getUuid()==null) {
	    		userService.add(user);
	    	}else {
	    		userService.update(user);
	    	}
	    	internalResponse = InternalResponse.success();
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}

	@RequestMapping("del")
	public InternalResponse del(int id){
		InternalResponse internalResponse = null;
	    try{
	    	userService.del(id);
	    	internalResponse = InternalResponse.success();
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}

	@RequestMapping("loginInfo")
	public InternalResponse loginInfo(HttpServletRequest request) {
		return InternalResponse.success().withBody(request.getSession().getAttribute("userkey"));
	}

	@RequestMapping("logout")
	public InternalResponse logout(HttpServletRequest request) {
		request.getSession().removeAttribute("userkey");
		return InternalResponse.success();
	}
	
	@RequestMapping(value = "/updatepassword", method = RequestMethod.POST)
	public InternalResponse updatepassword(@SessionAttribute(value = "userkey") User user, String password, String oldPassword ) {
		if(!user.getLogin_password().equals(oldPassword)){
			return InternalResponse.fail();
		}
		userService.updatePassword(user.getUuid(),password);
		user.setLogin_password(password);
		return InternalResponse.success();
	}

}
